package 线程池.阻塞队列;

import java.util.concurrent.BlockingQueue;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/8/15 11:17
 * @description:
 * @version:
 */
public class Producer implements Runnable{
    private final BlockingQueue<Task> queue;

    Producer(BlockingQueue<Task> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            for (int i = 1; i <= 10; i++) {
                Task task = new Task(i);
                queue.put(task); // 将任务放入阻塞队列
                System.out.println("Task " + task.getTaskId() + " is added to the queue");
                Thread.sleep(1000); // 模拟生产任务的耗时
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
